package com.fone.player.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import com.fone.player.R;
import com.fone.player.util.L;
import com.fone.player.util.ScreenUtil;

public class QuickPlayMoreOperationPopWindow implements OnClickListener {
	private PopupWindow mPopupWindow;
	private View mView;
	private Activity mActivity;
	private int popWidth = 160;
	private int popHeight = 84;
	private View rl_delete;
	private View rl_detail;
	private View rl_collect;
	private View rl_share;
	private IMoreOperationListener mListener;
	private boolean isLocal;

	public QuickPlayMoreOperationPopWindow(Activity activity, boolean isLocal) {
		this.mActivity = activity;
		this.isLocal = isLocal;
		popWidth = ScreenUtil.dp2px((float) popWidth);
		popHeight = ScreenUtil.dp2px((float) popHeight);
		mView = LayoutInflater.from(mActivity).inflate(
				R.layout.quick_play_more_operation_pop_view, null);
		initView();
	}

	public void setOperationListener(IMoreOperationListener listener) {
		this.mListener = listener;
	}

	@SuppressWarnings("deprecation")
	private void initView() {
		if (isLocal) {
			popWidth = popWidth / 2;
			popHeight = popHeight / 2;
		}

		rl_delete = mView.findViewById(R.id.rl_delete);
		rl_detail = mView.findViewById(R.id.rl_detail);
		rl_collect = mView.findViewById(R.id.rl_collect);
		rl_share = mView.findViewById(R.id.rl_share);

		rl_delete.setOnClickListener(this);
		rl_detail.setOnClickListener(this);
		rl_collect.setOnClickListener(this);
		rl_share.setOnClickListener(this);
		if (isLocal) {
			rl_share.setVisibility(View.GONE);
			rl_collect.setVisibility(View.GONE);
			rl_detail.setVisibility(View.GONE);
		} else {
			rl_share.setVisibility(View.VISIBLE);
			rl_collect.setVisibility(View.VISIBLE);
			rl_detail.setVisibility(View.VISIBLE);
		}
		mPopupWindow = new PopupWindow(mView, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		mPopupWindow.setWidth(popWidth);
		mPopupWindow.setHeight(popHeight);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 为了点击其他地方
																	// popupWindow能够消失
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				if(mListener!=null){
					mListener.dismiss();
				}
			}
		});
	}

	public void showPopupWindow(View view, boolean isLocal) {
		if (mPopupWindow != null) {
			L.i("TAG", "showPopupWindow", "mPopupWindow is null");
		}
		int[] location = new int[2];
		view.getLocationOnScreen(location);
		int x = location[0] + view.getWidth() - popWidth;
		int y = location[1] + view.getHeight();
		mPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_delete:
			if (mListener != null) {
				mListener.delete();
			}

			break;
		case R.id.rl_collect:
			if (mListener != null) {
				mListener.collect();
			}

			break;
		case R.id.rl_detail:
			if (mListener != null) {
				mListener.getDetail();
			}

			break;
		case R.id.rl_share:
			if (mListener != null) {
				mListener.share();
			}

			break;

		default:
			break;
		}
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
		}
	}

	public interface IMoreOperationListener {
		void delete();

		void collect();

		void getDetail();

		void share();
		
		void dismiss();
	}
}
